package com.njwb.Util;

import com.njwb.pojo.Bill;
import com.njwb.pojo.Supplier;
import com.njwb.pojo.SupplierBill;
import com.njwb.pojo.Users;

import javax.swing.table.TableModel;
import java.awt.*;
import java.sql.Date;
import java.sql.SQLException;
import java.util.ArrayList;

public class UtilService {
    private UtilDao utilDao = new UtilDao();
    private Io io = new Io();

    // ===================== 用户相关 =====================
    public String login(String username, String password, int role) {
        try {
            return utilDao.logn(username, password, role);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    public ArrayList<Users> getAllUsers() {
        try {
            return utilDao.getAllUsers();
        } catch (SQLException e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }

    public ArrayList<Users> getUsersByUsername(String name) {
        try {
            return utilDao.getUsersByUsername(name);
        } catch (SQLException e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }



    public boolean addUser(String username, String password, String gender, int age, String phone, String address, int role) {
        try {
            return utilDao.addUser(username, password, gender, age, phone, address, role);
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean updateUser(int id, String username, String password, String gender, int age, String phone, String address, int role) {
        try {
            return utilDao.updateUser(id, username, password, gender, age, phone, address, role);
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean deleteUser(int id) {
        try {
            return utilDao.deleteUser(id);
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    // ===================== 供应商相关 =====================
    public ArrayList<Supplier> getAllSupplier() {
        try {
            return utilDao.getAllSupplier();
        } catch (SQLException e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }
    public ArrayList<Supplier> getSupplierByName(String name){
        try {
            return utilDao.getSupplierByName(name);
        } catch (SQLException e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }
    public ArrayList<Bill> getBill(String product_name, Boolean is_paid){
        try {
            return utilDao.getBill(product_name,is_paid);
        } catch (SQLException e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }
    public boolean addSupplier(String name, String description, String contact_person, String phone, String address) {
        try {
            return utilDao.addSupplier(name, description, contact_person, phone, address);
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean updateSupplier(int supplier_id, String name, String description, String contact_person, String phone, String address) {
        try {
            return utilDao.updateSupplier(supplier_id, name, description, contact_person, phone, address);
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean deleteSupplier(int supplier_id) {
        try {
            return utilDao.deleteSupplier(supplier_id);
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    // ===================== 账单相关 =====================
    public ArrayList<Bill> getAllBill() {
        try {
            return utilDao.getAllBill();
        } catch (SQLException e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }

    public boolean addBill(String product_name, int quantity, String unit, Double amount,
                           Boolean is_paid, String supplier_name, String product_description, Date bill_time) {
        try {
            return utilDao.addBill(product_name, quantity, unit, amount, is_paid, supplier_name, product_description, bill_time);
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean modifyBill(int bill_id, String product_name, int quantity, String unit, Double amount,
                              Boolean is_paid, String supplier_name, String product_description, Date bill_time) {
        try {
            return utilDao.modifyBill(bill_id, product_name, quantity, unit, amount, is_paid, supplier_name, product_description, bill_time);
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean deleteBill(int bill_id) {
        try {
            return utilDao.deleteBill(bill_id);
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    public void exportTableToTxt(Component parent, TableModel tableModel){
        io.exportTableToTxt(parent,tableModel);
    }

    public void importSupplier(Component parent){
        io.importSupplier(parent, this);
    }

    public ArrayList<SupplierBill> getSupplierStatistics() {
        try {
            return utilDao.getSupplierStatistics();
        } catch (SQLException e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }
}
